<template>
  <j-modal
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    :title="title"
    :visible="visible"
    :width="width"
    cancelText="关闭"
    switchFullscreen
    @cancel="handleCancel"
    @ok="handleOk">
    <a-form-item>
      <j-date v-model="sjrq" class="query-group-cust" placeholder="请选择日期"></j-date>&nbsp;&nbsp;
      <a-button type="primary" icon="download" @click="handleExport()">导出</a-button>
    </a-form-item>
    <template slot="footer">
      <a-button type="primary" @click="close">关闭</a-button>
    </template>
  </j-modal>
</template>

<script>
import { ACCESS_TOKEN,TENANT_ID } from "@/store/mutation-types"
import Vue from 'vue'
import { Modal } from 'ant-design-vue'
import store from '@/store'
import { downFile } from '@api/manage'
export default {
  name: 'ExportModel',
  components: {},
  data() {
    return {
      model: {},
      sjrq: new Date(),
      title: '选择日期',
      width: 600,
      visible: false,
      disableSubmit: false,
      url: {
        exportXlsUrl: "reportInfo/llbbDepositLl/exportXls",
      },
    }
  },
  created() {
  },
  computed: {
    //token header
    tokenHeader(){
      let head = {'X-Access-Token': Vue.ls.get(ACCESS_TOKEN)}
      let tenantid = Vue.ls.get(TENANT_ID)
      if(tenantid){
        head['tenant-id'] = tenantid
      }
      return head;
    },
  },
  props: {
    handleAdd: {
      type: Function,
      default: null
    }
  },
  methods: {
    showModal() {
      var date=new Date()
      this.sjrq = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    selectDate(){
      var date=new Date()
      this.sjrq = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()
    },
    handleOk(e) {
      if (this.sjrq == undefined || this.sjrq == '') {
        this.$message.warning('请选择数据日期!')
        return
      }
    },
    submitCallback() {
      this.$emit('ok')
      this.visible = false
    },
    handleExport: function(){
      var vdate="";
      if(this.sjrq==''){
        var date=new Date()
        this.sjrq = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()
      }
      vdate=this.sjrq;
      let fileName = "阜新银行存款利率自律统计月报";
      let param = new Object();
      param['sjrq']=vdate;
      downFile(this.url.exportXlsUrl,param).then((data)=>{
        if (!data) {
          this.$message.warning("文件下载失败")
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data],{type: 'application/vnd.ms-excel'}), fileName+'.xlsx')
        }else{
          let url = window.URL.createObjectURL(new Blob([data],{type: 'application/vnd.ms-excel'}))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName+'.xlsx')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link); //下载完成移除元素
          window.URL.revokeObjectURL(url); //释放掉blob对象
        }
      });
    },
    handleCancel() {
      this.sjrq = new Date()
      this.close()
    },
    customRequest(data){
      console.log(data)
      var list_name = "阜新银行存款利率自律统计月报导入结果.xlsx";
      let formData = new FormData();
      formData.append("file", data.file);
      formData.append("sjrq",this.sjrq)
      this.axios.post(`${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`,formData,{'responseType': "blob"},).then(response=>{
        this.export_excel_file(response, list_name);
      })
    },
    export_excel_file(export_data,list_name){
      this.loading = true;
      let link = document.createElement("a");
      //    type就是blob的type,是MIME类型的，可以自己查看MIME类型都有哪些
      let blogw = new Blob([export_data],{type:"application/vnd.ms-excel;charset=utf-8"})
      let objectUrl = window.URL.createObjectURL(blogw);    //创建一个新的url对象
      link.href = objectUrl;
      let file_name = list_name
      link.download = file_name;    //  下载的时候自定义的文件名
      link.click();
      window.URL.revokeObjectURL(objectUrl);   //为了更好地性能和内存使用状况，应该在适当的时候释放url.
      this.sjrq = new Date()
      this.$emit('ok')
      this.visible = false
      this.loading = false;
    },
  }
}
</script>
<style scoped>

</style>